package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.dto.InvoiceTitleDTO;
import com.eastfair.pay.dto.InvoiceTitlePageQuery;
import com.eastfair.pay.dto.InvoiceTitleSaveDTO;
import com.eastfair.pay.dto.InvoiceTitleUpdateDTO;
import com.eastfair.pay.entity.InvoiceTitle;
import com.eastfair.pay.service.InvoiceTitleService;
import com.eastfair.pay.vo.InvoiceTitleVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 发票抬头
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/invoiceTitle")
@Api(value = "InvoiceTitle", tags = "发票抬头")
@PreAuth(replace = "pay:invoiceTitle:")
public class InvoiceTitleController extends SuperController<InvoiceTitleService, Long, InvoiceTitle, InvoiceTitlePageQuery, InvoiceTitleSaveDTO, InvoiceTitleUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<InvoiceTitle> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<InvoiceTitle> invoiceTitleList = list.stream().map((map) -> {
            InvoiceTitle invoiceTitle = InvoiceTitle.builder().build();
            //TODO 请在这里完成转换
            return invoiceTitle;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(invoiceTitleList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<InvoiceTitlePageQuery> params) {
        InvoiceTitlePageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 保存修改
     * @param invoiceTitleDTO
     * @return
     */
    @PostMapping("/saveOrUpdateInvoiceTitle")
    public R saveOrUpdateInvoiceTitle(@Validated @RequestBody InvoiceTitleDTO invoiceTitleDTO){
        baseService.saveOrUpdate(invoiceTitleDTO);
        return R.success();
    }

    /**
     * 查询
     * @param invoiceTitleDTO
     * @return
     */
    @PostMapping("/queryInvoiceTitle")
    public R<List<InvoiceTitleVO>> query(@RequestBody InvoiceTitleDTO invoiceTitleDTO){
        List<InvoiceTitle> list = baseService.list(invoiceTitleDTO);
        List<InvoiceTitleVO> invoiceTitleVOS = ConvertUtil.convertList(list, InvoiceTitleVO.class);
        return R.success(invoiceTitleVOS);
    }
}
